/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Implementation.SourceCode.entities;

import Implementation.SourceCode.Utilities.HibernateUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author mrapple
 */
@Entity
@Table(name = "MATERIAL")
public class Material implements Serializable {

    private int id;
    private String name;
    private String shortName;
    private String code;
    private String wareHouse;

    public Material() {
    }

    public Material(int id, String name, String shortName, String code) {
        this.id = id;
        this.name = name;
        this.shortName = shortName;
        this.code = code;
    }

    public Material(String name, String shortName, String code, String wareHouse) {
        this.name = name;
        this.shortName = shortName;
        this.code = code;
        this.wareHouse = wareHouse;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "short_name")
    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    @Column(name = "code")
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name ="ware_house")
    public String getWareHouse() {
        return wareHouse;
    }

    public void setWareHouse(String wareHouse) {
        this.wareHouse = wareHouse;
    }
    
    

    @Id
    @GeneratedValue
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void save() {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.save(this);
            tx.commit();
            session.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

//    @Transient
    public boolean reset() {
        boolean retVal = false;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.createSQLQuery("delete from Material").executeUpdate();
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return retVal;
    }

    @Transient
    public List<Material> getAllMaterial() {
        List<Material> lstMaterial = new ArrayList<Material>();
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            lstMaterial = session.createQuery("from Material mat order by mat.wareHouse, mat.id").list();
            tx.commit();
            session.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return lstMaterial;
    }

    public void delete() {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(this);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
